package com.wavehi.homework.second;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @Author WaveHi
 * @Date 27/8/2021 下午 7:07
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 * @Description Second homework : Demonstrate the use of functional interface.
 */
public class FunctionalInterfaceDemo1 {

    public static void main(String[] args) {
        //打印消费信息
        Consumer<Double> consumer = money -> System.out.println("Your have spend " + money + " yuan");
        consumer.accept(120.0);

        //获取验证码
        Supplier<String> supplier = () -> {
            StringBuilder stringBuilder = new StringBuilder(10);
            Random random = new Random();
            for (int i = 0;i < 11;i++) {
                stringBuilder.append(random.nextInt(10));
            }
            return new String(stringBuilder);
        };
        System.out.println("The code you get is :" + supplier.get());

        //获取字符串长度
        Function<String,Integer> function = s -> s.trim().length();
        System.out.println("The length pf string \'Wow!\' is :" + function.apply("Wow!"));

        //获取特定字符串构成的集合
        Predicate<String> predicate = s -> s.length() > 5;
        Function<List<String>,List<String>> functionUp = l -> {
            ArrayList<String> list = new ArrayList<>(16);
            for(String s : l) {
                if(predicate.test(s)){
                    list.add(s);
                }
            }
            return list;
        };
        //获取测试数据
        List<String> list = Arrays.asList("Wow!", "Hello!", "Yes!", "Ok!", "No!");
        //测试
        List<String> apply = functionUp.apply(list);
        //输出信息
        for(String s : apply) {
            System.out.println(s);
        }

    }

}
//小结：Lambda本质式函数式接口的一个实例。